<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Tacopie: includes/tacopie/utils/thread_pool.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="tacopie_logo.jpg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Tacopie
   &#160;<span id="projectnumber">3.0.0</span>
   </div>
   <div id="projectbrief">Tacopie is a TCP Client &amp; Server C++11 library.</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_09e761304027c904456130627fd4dcf5.html">includes</a></li><li class="navelem"><a class="el" href="dir_9a013686e2522dd95763481288c1f90f.html">tacopie</a></li><li class="navelem"><a class="el" href="dir_38b559f7600c1df65fbc4b39f02eb3f8.html">utils</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">thread_pool.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">// MIT License</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment">// Copyright (c) 2016-2017 Simon Ninon &lt;simon.ninon@gmail.com&gt;</span></div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment">// Permission is hereby granted, free of charge, to any person obtaining a copy</span></div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment">// of this software and associated documentation files (the &quot;Software&quot;), to deal</span></div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment">// in the Software without restriction, including without limitation the rights</span></div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment">// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span></div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment">// copies of the Software, and to permit persons to whom the Software is</span></div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="comment">// furnished to do so, subject to the following conditions:</span></div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment">// The above copyright notice and this permission notice shall be included in all</span></div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment">// copies or substantial portions of the Software.</span></div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment">// THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span></div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="comment">// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span></div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment">// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span></div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment">// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span></div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="comment">// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span></div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;<span class="comment">// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span></div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="comment">// SOFTWARE.</span></div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;<span class="preprocessor">#pragma once</span></div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;<span class="preprocessor">#include &lt;atomic&gt;</span></div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="preprocessor">#include &lt;condition_variable&gt;</span></div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;<span class="preprocessor">#include &lt;functional&gt;</span></div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;<span class="preprocessor">#include &lt;list&gt;</span></div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;<span class="preprocessor">#include &lt;mutex&gt;</span></div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;<span class="preprocessor">#include &lt;queue&gt;</span></div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;<span class="preprocessor">#include &lt;thread&gt;</span></div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;<span class="preprocessor">#include &lt;vector&gt;</span></div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespacetacopie.html">tacopie</a> {</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;<span class="keyword">namespace </span>utils {</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;</div><div class="line"><a name="l00041"></a><span class="lineno"><a class="line" href="classtacopie_1_1utils_1_1thread__pool.html">   41</a></span>&#160;<span class="keyword">class </span><a class="code" href="classtacopie_1_1utils_1_1thread__pool.html">thread_pool</a> {</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;<span class="keyword">public</span>:</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;  <span class="keyword">explicit</span> <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html#aa0068f000b8ecb21b2b0a0c50f77877c">thread_pool</a>(std::size_t nb_threads);</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;  <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html#accddfc6fad613b0c6d407028799abfc5">~thread_pool</a>(<span class="keywordtype">void</span>);</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;  <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html#aa0068f000b8ecb21b2b0a0c50f77877c">thread_pool</a>(<span class="keyword">const</span> <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html">thread_pool</a>&amp;) = <span class="keyword">delete</span>;</div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;  <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html">thread_pool</a>&amp; <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html#aa0077461da4da6ba3f4dcac029351234">operator=</a>(<span class="keyword">const</span> <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html">thread_pool</a>&amp;) = <span class="keyword">delete</span>;</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;</div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;<span class="keyword">public</span>:</div><div class="line"><a name="l00064"></a><span class="lineno"><a class="line" href="classtacopie_1_1utils_1_1thread__pool.html#a8ae8886fdeaa8e5c0abad12626a47296">   64</a></span>&#160;  <span class="keyword">typedef</span> std::function&lt;void()&gt; <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html#a8ae8886fdeaa8e5c0abad12626a47296">task_t</a>;</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;</div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;  <span class="keywordtype">void</span> <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html#a450bee2b7b2cd0aa0bc3935c8adc9ace">add_task</a>(<span class="keyword">const</span> task_t&amp; task);</div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;</div><div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;  <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html">thread_pool</a>&amp; <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html#afb7befd9fa5f1fbd44423fddc8cbc5d3">operator&lt;&lt;</a>(<span class="keyword">const</span> task_t&amp; task);</div><div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;</div><div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;  <span class="keywordtype">void</span> <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html#a8874d7040d12fbe446519bc9f2bcff37">stop</a>(<span class="keywordtype">void</span>);</div><div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;</div><div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;<span class="keyword">public</span>:</div><div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;  <span class="keywordtype">bool</span> <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html#a77a647a75be188a5c5f83b922c061107">is_running</a>(<span class="keywordtype">void</span>) <span class="keyword">const</span>;</div><div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;</div><div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;<span class="keyword">public</span>:</div><div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;  <span class="keywordtype">void</span> <a class="code" href="classtacopie_1_1utils_1_1thread__pool.html#a7c7ae922cedff8fa323828ebb6dea829">set_nb_threads</a>(std::size_t nb_threads);</div><div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;</div><div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;<span class="keyword">private</span>:</div><div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;  <span class="keywordtype">void</span> run(<span class="keywordtype">void</span>);</div><div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;</div><div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;  std::pair&lt;bool, task_t&gt; fetch_task_or_stop(<span class="keywordtype">void</span>);</div><div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;</div><div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;  <span class="keywordtype">bool</span> should_stop(<span class="keywordtype">void</span>) <span class="keyword">const</span>;</div><div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;</div><div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;<span class="keyword">private</span>:</div><div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;  std::list&lt;std::thread&gt; m_workers;</div><div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;</div><div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;  std::atomic&lt;std::size_t&gt; m_max_nb_threads = ATOMIC_VAR_INIT(0);</div><div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;</div><div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;  std::atomic&lt;std::size_t&gt; m_nb_running_threads = ATOMIC_VAR_INIT(0);</div><div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;</div><div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;  std::atomic&lt;bool&gt; m_should_stop = ATOMIC_VAR_INIT(<span class="keyword">false</span>);</div><div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;</div><div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;  std::queue&lt;task_t&gt; m_tasks;</div><div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;</div><div class="line"><a name="l00159"></a><span class="lineno">  159</span>&#160;  std::mutex m_tasks_mtx;</div><div class="line"><a name="l00160"></a><span class="lineno">  160</span>&#160;</div><div class="line"><a name="l00164"></a><span class="lineno">  164</span>&#160;  std::condition_variable m_tasks_condvar;</div><div class="line"><a name="l00165"></a><span class="lineno">  165</span>&#160;};</div><div class="line"><a name="l00166"></a><span class="lineno">  166</span>&#160;</div><div class="line"><a name="l00167"></a><span class="lineno">  167</span>&#160;} <span class="comment">// namespace utils</span></div><div class="line"><a name="l00168"></a><span class="lineno">  168</span>&#160;</div><div class="line"><a name="l00169"></a><span class="lineno">  169</span>&#160;} <span class="comment">// namespace tacopie</span></div><div class="ttc" id="classtacopie_1_1utils_1_1thread__pool_html_accddfc6fad613b0c6d407028799abfc5"><div class="ttname"><a href="classtacopie_1_1utils_1_1thread__pool.html#accddfc6fad613b0c6d407028799abfc5">tacopie::utils::thread_pool::~thread_pool</a></div><div class="ttdeci">~thread_pool(void)</div><div class="ttdoc">dtor </div></div>
<div class="ttc" id="classtacopie_1_1utils_1_1thread__pool_html_aa0068f000b8ecb21b2b0a0c50f77877c"><div class="ttname"><a href="classtacopie_1_1utils_1_1thread__pool.html#aa0068f000b8ecb21b2b0a0c50f77877c">tacopie::utils::thread_pool::thread_pool</a></div><div class="ttdeci">thread_pool(std::size_t nb_threads)</div></div>
<div class="ttc" id="classtacopie_1_1utils_1_1thread__pool_html_aa0077461da4da6ba3f4dcac029351234"><div class="ttname"><a href="classtacopie_1_1utils_1_1thread__pool.html#aa0077461da4da6ba3f4dcac029351234">tacopie::utils::thread_pool::operator=</a></div><div class="ttdeci">thread_pool &amp; operator=(const thread_pool &amp;)=delete</div><div class="ttdoc">assignment operator </div></div>
<div class="ttc" id="classtacopie_1_1utils_1_1thread__pool_html_a8ae8886fdeaa8e5c0abad12626a47296"><div class="ttname"><a href="classtacopie_1_1utils_1_1thread__pool.html#a8ae8886fdeaa8e5c0abad12626a47296">tacopie::utils::thread_pool::task_t</a></div><div class="ttdeci">std::function&lt; void()&gt; task_t</div><div class="ttdef"><b>Definition:</b> thread_pool.hpp:64</div></div>
<div class="ttc" id="namespacetacopie_html"><div class="ttname"><a href="namespacetacopie.html">tacopie</a></div><div class="ttdef"><b>Definition:</b> io_service.hpp:48</div></div>
<div class="ttc" id="classtacopie_1_1utils_1_1thread__pool_html_a450bee2b7b2cd0aa0bc3935c8adc9ace"><div class="ttname"><a href="classtacopie_1_1utils_1_1thread__pool.html#a450bee2b7b2cd0aa0bc3935c8adc9ace">tacopie::utils::thread_pool::add_task</a></div><div class="ttdeci">void add_task(const task_t &amp;task)</div></div>
<div class="ttc" id="classtacopie_1_1utils_1_1thread__pool_html_a8874d7040d12fbe446519bc9f2bcff37"><div class="ttname"><a href="classtacopie_1_1utils_1_1thread__pool.html#a8874d7040d12fbe446519bc9f2bcff37">tacopie::utils::thread_pool::stop</a></div><div class="ttdeci">void stop(void)</div></div>
<div class="ttc" id="classtacopie_1_1utils_1_1thread__pool_html_afb7befd9fa5f1fbd44423fddc8cbc5d3"><div class="ttname"><a href="classtacopie_1_1utils_1_1thread__pool.html#afb7befd9fa5f1fbd44423fddc8cbc5d3">tacopie::utils::thread_pool::operator&lt;&lt;</a></div><div class="ttdeci">thread_pool &amp; operator&lt;&lt;(const task_t &amp;task)</div></div>
<div class="ttc" id="classtacopie_1_1utils_1_1thread__pool_html_a7c7ae922cedff8fa323828ebb6dea829"><div class="ttname"><a href="classtacopie_1_1utils_1_1thread__pool.html#a7c7ae922cedff8fa323828ebb6dea829">tacopie::utils::thread_pool::set_nb_threads</a></div><div class="ttdeci">void set_nb_threads(std::size_t nb_threads)</div></div>
<div class="ttc" id="classtacopie_1_1utils_1_1thread__pool_html"><div class="ttname"><a href="classtacopie_1_1utils_1_1thread__pool.html">tacopie::utils::thread_pool</a></div><div class="ttdef"><b>Definition:</b> thread_pool.hpp:41</div></div>
<div class="ttc" id="classtacopie_1_1utils_1_1thread__pool_html_a77a647a75be188a5c5f83b922c061107"><div class="ttname"><a href="classtacopie_1_1utils_1_1thread__pool.html#a77a647a75be188a5c5f83b922c061107">tacopie::utils::thread_pool::is_running</a></div><div class="ttdeci">bool is_running(void) const</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
